<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/3/21
  Time: 16:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>My web</title>
</head>
<body>

<%--判断用户是否登录--%>
<%
    out.write("<h1>用户管理系统</h1>");
    if (session.getAttribute("isLogin") == null) {

        //说明没有登录，跳转到login.jsp
        response.sendRedirect("login.jsp");
    }

%>

<style type="text/css">
    table {
        border: 1px solid #000;
        border-collapse: collapse;
    }

    td, th {
        border: 1px solid #000;
        padding: 10px;
    }

</style>

<a href="addUser.jsp">添加用户</a>

<table>
    <thead>
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>密码</td>
        <td>操作1</td>
        <td>操作2</td>
    </tr>
    </thead>
    <tbody>

    <%

        class User {
            int id;
            String username;
            String password;
        }
        List<User> userList = new ArrayList<User>();

        //3+1
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        String sql = "select * from t_users";

        //加载驱动程序
        Class.forName("com.mysql.jdbc.Driver");

        //获取数据库连接
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",//URL
                "root",
                "123456"
        );

        statement = connection.createStatement();//访问数据库

        resultSet = statement.executeQuery(sql);//执行SQL语句
        while (resultSet.next()) {

            int id = resultSet.getInt(1);
            String username = resultSet.getString(2);
            String password = resultSet.getString(3);

            User user = new User();
            user.id = id;
            user.username = username;
            user.password = password;

            userList.add(user);

        }
        resultSet.close();
        statement.close();
        connection.close();

        for (User user : userList) {

            out.write("<tr>"+
                    "<td>" + user.id + "</td>" +
                    "<td>" + user.username + "</td>" +
                    "<td>" + user.password + "</td>" +
                    "<td><a href='deleteUserHandler.jsp?id=" + user.id + "'>删除</a></td>" +
                    "<td><a href='userUpdate.jsp?id=" + user.id + "&username=" + user.username + "&password=" + user.password + "'>更新</a></td>"+
                    "</tr>"
                   );
        }
    %>
    </tbody>
</table>
<%--jsp include指令，包含的是静态信息--%>
<%--<%@include file="footer.jsp" %>--%>

<%--&lt;%&ndash;jsp前缀; include包含的是动态的信息--%>
<jsp:include page="footer.jsp">
    <jsp:param name="year" value="2018"/>
</jsp:include>

</body>
</html>
